package com.mlc.api.controller;

import com.mlc.data.entity.Details;
import com.mlc.core.service.DetailsService;
import com.alibaba.fastjson.JSONObject;
import com.mlc.utils.commom.ResponseResult;
import com.mlc.utils.enums.CodeType;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author FireWang
 * @date 2022/03/14 22:43:06
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/detail")
public class DetailsController {
    private DetailsService detailsService;
    private static JSONObject result;

    /**
     * 获取全部数据列表
     *
     * @return
     */
    @GetMapping("/list")
    public JSONObject getAll() {
        try {
            List<Details> detailsList = detailsService.getAll();
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
            result.put("list", detailsList);
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return
     */
    @GetMapping
    public JSONObject getById(@RequestParam Integer id) {
        try {
            Details details = detailsService.getById(id);
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
            result.put("data", details);
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 新建
     *
     * @param details
     * @return
     */
    @PostMapping
    public JSONObject insert(@RequestBody Details details) {
        try {
            detailsService.insert(details);
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 更新
     *
     * @param details
     * @return
     */
    @PutMapping
    public JSONObject update(@RequestBody Details details) {
        try {
            detailsService.update(details);
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public JSONObject deleteById(@RequestParam Integer id) {
        try {
            detailsService.deleteById(id);
            result = ResponseResult.build(CodeType.SUCCESS, "成功！");
        } catch (Exception e) {
            log.error(e.getMessage());
            result = ResponseResult.build(CodeType.FAIL, "失败：" + e.getMessage());
        }
        return result;
    }
}